package com.example.javaservice.common;
import lombok.Data;
import java.io.Serializable;

/**规定统一的json响应格式*/
@Data
public class Result<T> implements Serializable {
    private Integer code;    // 状态码
    private String message;  // 提示信息
    private T data;         // 数据
    private Long timestamp; // 时间戳

    public Result() {
        this.timestamp = System.currentTimeMillis();
    }

    // 成功静态方法
    public static <T> Result<T> success() {
        Result<T> result = new Result<>();
        result.setCode(200);
        result.setMessage("成功");
        return result;
    }

    public static <T> Result<T> success(String message) {
        Result<T> result = new Result<>();
        result.setCode(200);
        result.setMessage("成功");
        result.setMessage(message);
        return result;
    }

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setCode(200);
        result.setMessage("成功");
        result.setData(data);
        return result;
    }

    public static <T> Result<T> success(String message, T data) {
        Result<T> result = new Result<>();
        result.setCode(200);
        result.setMessage(message);
        result.setData(data);
        return result;
    }

    // 错误静态方法
    public static <T> Result<T> error(Integer code, String message) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    public static <T> Result<T> error(String message) {
        return error(500, message);
    }
}